﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Trackr.Core.DataProvider
{
	public static class Extensions
	{
		private static System.ComponentModel.TypeConverter _converter;
		public static SqlDbType GetDbType(this Type type)
		{
			var param = new System.Data.SqlClient.SqlParameter();
			if (_converter == null)
			{
				_converter = System.ComponentModel.TypeDescriptor.GetConverter(param.DbType);
			}
			try
			{
				param.DbType = (DbType)_converter.ConvertFrom(type.Name);
				return param.SqlDbType;
			}
			catch
			{
				if (type == typeof(int))
					return SqlDbType.Int;
				if (type == typeof(bool))
					return SqlDbType.Bit;
				if (type == typeof(string))
					return SqlDbType.NVarChar;
				if (type == typeof(DateTime))
					return SqlDbType.DateTime;
				if (type == typeof(TimeSpan))
					return SqlDbType.DateTimeOffset;
				return SqlDbType.NVarChar;
			}
		}
	}
}
